package cn.openread.starter.utils.sms;

import cn.openread.starter.utils.kits.DateKits;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;

/**
 * 梦网发送短信的数据模型
 *
 * @author Simon
 */
@EqualsAndHashCode(callSuper = true)
@Builder
@Data
public class MengWangSmsModel extends SmsSendModel {

    @JSONField(serialize = false)
    private String domain;

    @JSONField(name = "userid")
    private String userId;

    private String pwd;

    @JSONField(serialize = false)
    private String timestamp;

    private String content;


    protected String toSendString() {
        String jsonString = JSON.toJSONString(this);
        System.out.println(jsonString);
        return jsonString;
    }

    @Override
    protected void doValidate() {
        //step 1.基本参数验证
        checkNotBlank(super.getMobile(), "mobile is blank");
        checkNotBlank(content, "content is blank");
        checkNotBlank(domain, "domain is blank");
        checkNotBlank(userId, "userId is blank");
        checkNotBlank(pwd, "pwd is blank");
    }

    @Override
    protected void doBizInit() {
        this.timestamp = DateKits.formatDate(new Date(), DateKits.DATE_TIME_FORMAT_7);
//        String origin = userId + "00000000" + pwd + this.timestamp;
//        this.pwd = DigestUtils.md5Hex(origin);
        try {
            this.content = URLEncoder.encode(content, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
